﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuildingTomorow;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using System.Collections.ObjectModel;

namespace Testing_components
{
   public class ChartSensorGrouping
    {
       public string Type { get; set; }
       public List<PresentableSensor> Sensors { get; set; }
       public RadChart chart { get; set; }
       private Dictionary<PresentableSensor, List<DataPoint>> _VisiblePoints = new Dictionary<PresentableSensor,List<DataPoint>>();
       private bool _labels;
       public void PrepareSensors(DateTime fromTime, DateTime toTime, bool average)
       {
           foreach (var item in Sensors)
           {
               item.UpdateData(fromTime, toTime);
           }
           if (average)
           {
               PresentableSensor AverageSensor = new PresentableSensor();
               AverageSensor.Name = "Average " + Sensors.First().Type;
               AverageSensor.Type = Sensors.First().Type;
               AverageSensor.Measurment = Sensors.First().Measurment;
               ObservableCollection<PresentableDataPoint> DataPoints = new ObservableCollection<PresentableDataPoint>();
               foreach (var sen in Sensors)
               {
                   if (DataPoints.Count == 0)
                   {
                       DataPoints = sen.DataPoints;
                   }
                   else
                   {
                       for (int i = 0; i < sen.DataPoints.Count; i++)
                       {
                           DataPoints[i].Value = Math.Round((DataPoints[i].Value + sen.DataPoints[i].Value) / 2, 2);
                       }
                   }
               }
               AverageSensor.DataPoints = DataPoints;
               Sensors = new List<PresentableSensor>();
               Sensors.Add(AverageSensor);
           } try
           {
               _labels = (Sensors.First().DataPoints.Last().TimeStamp.ToOADate() - Sensors.First().DataPoints.First().TimeStamp.ToOADate()) > 0.6;
           }
           catch
           {
               _labels = false;
           }
           foreach (var item in Sensors)
           {
               List<DataPoint> dtps = new List<DataPoint>();
               foreach (var dtap in item.DataPoints)
               {
                   DataPoint datap = new DataPoint() { YValue = dtap.Value, XValue = dtap.TimeStamp.ToOADate() };
                   datap.Label = dtap.Value + item.Measurment;
                   datap.IsDateTime = true;
                   datap.Tooltip = item.Type + ": " + dtap.Value + item.Measurment + "\n" + dtap.TimeStamp.ToShortDateString() + " " + dtap.TimeStamp.ToShortTimeString();
                   dtps.Add(datap);
               }
               _VisiblePoints.Add(item, dtps);
           }
       }

       public void Prepare()
       {
           chart.Opacity = 0;
           chart.DefaultView.ChartArea.AxisX.IsDateTime = true;
           chart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Inside;
           chart.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;
           chart.DefaultView.ChartArea.AxisX.IsDateTime = true;
           chart.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "hh-mm-dd-MMM";
           chart.DefaultView.ChartArea.ZoomScrollSettingsX.MinZoomRange = 0.1;
           chart.DefaultView.ChartArea.SmartLabelsEnabled = true;
           chart.DefaultView.ChartArea.EnableAnimations = false;
           chart.DefaultView.ChartArea.EnableStripLinesAnimation = false;
           //zoom
           chart.DefaultView.ChartArea.ZoomScrollSettingsX.ScrollMode = ScrollMode.ScrollAndZoom;

           chart.MaxHeight = 300;
           StyleManager.SetTheme(chart, new MetroTheme());
           foreach (var item in Sensors)
           {
               DataSeries lineSeries = new DataSeries();
               lineSeries.LegendLabel = item.Name;
               lineSeries.Definition = new LineSeriesDefinition();

               //interactivity (hover)
               lineSeries.Definition.InteractivitySettings.HoverScope = InteractivityScope.Series;

               //Tool tips
               try
               {
                       lineSeries.Definition.ShowItemLabels = !_labels;
               }
               catch { }
               lineSeries.Definition.ShowItemToolTips = true;
               // lineSeries.Definition.ItemToolTipFormat = item.Type + ": #Y°C\n #X";

               //foreach (var dtap in item.DataPoints)
               //{
               //    DataPoint datap = new DataPoint() { YValue = dtap.Value, XValue = dtap.TimeStamp.ToOADate() };
               //    datap.Label = dtap.Value + item.Measurment;
               //    datap.IsDateTime = true;
               //    datap.Tooltip = item.Type + ": " + dtap.Value + item.Measurment + "\n" + dtap.TimeStamp.ToShortDateString() + " " + dtap.TimeStamp.ToShortTimeString();
               //    lineSeries.Add(datap);
               //}

               List<DataPoint> dtps = _VisiblePoints[item];
               lineSeries.AddRange(dtps);
               chart.DefaultView.ChartArea.DataSeries.Add(lineSeries);
           }

           chart.Opacity = 1;
       }

    }
}
